package com.ccf.business.etl.flow;

import com.alibaba.fastjson.JSON;
import com.component.api.constants.ComponentCodeConstants;
import com.component.api.constants.ComponentConstants;
import com.component.api.model.dto.DatabaseParamDTO;
import com.component.api.model.flow.NodeInfo;
import com.service.database.config.DataBase;
import com.service.database.config.DatabaseConfig;

import java.util.Map;

/**
 * 描述：
 *
 * @author xianggj
 * @Date 2021/11/30 14:45
 **/
public class DataOutputParamHandler extends ParamHandler {


    /**
     * 整理参数
     * @return
     */
    @Override
    public Map<String, String> finishingParam() {
        Map<String, String> params = getParams();
        String sourceId = params.get(ComponentConstants.SOURCE_ID);
        DatabaseConfig byId = databaseClient.getConfig(sourceId, null);
        if (byId == null){
            throw new RuntimeException("数据源不存在");
        }
        //注意前后顺序
        if (DataBase.MYSQL.name().equals(byId.getType())){
            nodeDTO.setCode(ComponentCodeConstants.MYSQL_OUTPUT);
        } else if (DataBase.POSTGRESQL.name().equals(byId.getType())){
            nodeDTO.setCode(ComponentCodeConstants.PG_OUTPUT);
        } else if (DataBase.ORACLE.name().equals(byId.getType())){
            nodeDTO.setCode(ComponentCodeConstants.ORACLE_OUTPUT);
        }
        DatabaseParamDTO databaseParamDTO = DatabaseParamDTO.builder()
                .type(byId.getType().name())
                .driver(byId.getDriver())
                .userName(byId.getUserName())
                .pwd(byId.getUserPwd())
                .schema(byId.getSchema())
                .tableName(params.get(ComponentConstants.TABLE_NAME))
                .dbName(byId.getDbName())
                .url(byId.getUrl())
                .build();
        params.put(ComponentConstants.CONFIG, JSON.toJSONString(databaseParamDTO));
        return params;
    }

    /**
     * 复写这个方法
     * @return
     */
    @Override
    public NodeInfo get(){
        NodeInfo nodeInfo = NodeInfo.builder().name(nodeDTO.getName())
                .id(nodeDTO.getId()).rule(nodeDTO.getCode()).build();
        nodeInfo.setParam(analysis());
        //重设rule
        nodeInfo.setRule(nodeDTO.getCode());
        return nodeInfo;
    }
}
